﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using OnlineAuction.DataAccess;
using OnlineAuction.Entity;

namespace OnlineAuction.Business
{
    public class AuctionBO
    {
        public string GetConnection() {
            AuctionDA da = new AuctionDA();
            return da.GetConnection();
        }
        public List<AuctionDTO> ViewAuction()
        {
            List<AuctionDTO> list = new List<AuctionDTO>();
            AuctionDA da = new AuctionDA();
            DataSet ds = da.ViewAuction();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string[] time = row["LeftHour"].ToString().Split(':');

                list.Add(new AuctionDTO(int.Parse(row["AuctionID"].ToString()),
                    row["ItemName"].ToString(), row["ItemImage1"].ToString(),
                    double.Parse(row["Price"].ToString()), row["LeftDay"].ToString(),
                    time[0], time[1], time[2]));
            }
            return list;
        }

        public AuctionDTO ViewDetailItem(int AuctionID)
        {
            //Get details of item
            List<AuctionDTO> list = new List<AuctionDTO>();
            AuctionDA da = new AuctionDA();
            DataRow row = da.ViewDetailItem(AuctionID).Tables[0].Rows[0];
            //Convert datarow to ItemsE
            string[] time = row["LeftHour"].ToString().Split(':');
            AuctionDTO auction = new AuctionDTO(
                int.Parse(row["AuctionID"].ToString()),
                row["ItemName"].ToString(),
                row["ItemImage1"].ToString(),
                row["ItemImage2"].ToString(),
                row["ItemImage3"].ToString(),
                row["ItemImage4"].ToString(),
                row["ItemDescription"].ToString(),
                row["Seller"].ToString(),
                row["LeftDay"].ToString(),
                time[0], time[1], time[2],
                row["UserName"].ToString(),
                double.Parse(row["Price"].ToString()),
                row["AuctionTypeName"].ToString(),
                row["BidIncrement"].ToString());
            return auction;
        }

        public bool FinishAuctionSessionImmediately(int auctionID)
        {
            AuctionDA da = new AuctionDA();
            return da.FinishAuctionSessionImmediately(auctionID);
        }

        public List<AuctionDTO> Statistic(string CategoryID, string TimeFrom, string TimeTo,
                string PriceFrom, string PriceTo, string AuctionStateID)
        {
            List<AuctionDTO> list = new List<AuctionDTO>();
            AuctionDA da = new AuctionDA();
            DataSet ds = da.Statistic(
                CategoryID.CompareTo("") == 0 ? 0 : int.Parse(CategoryID),
                TimeFrom, TimeTo,
                PriceFrom.CompareTo("") == 0 ? 0 : double.Parse(PriceFrom),
                PriceTo.CompareTo("") == 0 ? 0 : double.Parse(PriceTo),
                int.Parse(AuctionStateID));
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                list.Add(new AuctionDTO(
                        int.Parse(row["AuctionID"].ToString()),
                        row["ItemName"].ToString(),
                        row["AuctionStateName"].ToString()));
            }
            return list;
        }
        public List<AuctionDTO> GetOnlineAuctions(string search)
        {
            List<AuctionDTO> list = new List<AuctionDTO>();
            AuctionDA da = new AuctionDA();
            DataSet ds = da.GetOnlineAuctions(search);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                list.Add(new AuctionDTO(int.Parse(row["AuctionID"].ToString()),
                    row["ItemName"].ToString(), row["TimeFinishAuction"].ToString(),
                    row["UserName"].ToString()));
            }
            return list;
        }

        public bool BlockItemAndAuction(int AuctionID)
        {
            //Block Item And Aution
            AuctionDA da = new AuctionDA();
            return da.BlockItemAndAuction(AuctionID);
        }

        public List<string> GetYear()
        {
            AuctionDA da = new AuctionDA();
            List<string> list = new List<string>();
            list.Add("");
            list.AddRange(da.GetYear());
            return list;
        }

        public List<string> GetMonth(string Year)
        {
            AuctionDA da = new AuctionDA();
            List<string> list = new List<string>();
            list.Add("");
            list.AddRange(da.GetMonth(Year));
            return list;
        }

        public List<string> GetDay(string Year, string Month)
        {
            AuctionDA da = new AuctionDA();
            List<string> list = new List<string>();
            list.Add("");
            list.AddRange(da.GetDay(Year, Month));
            return list;
        }

        public List<AuctionDTO> Revenue(string Year, string Month, string Day)
        {
            AuctionDA da = new AuctionDA();
            return da.Revenue(Year, Month, Day);
        }
    }
}
